<?php
    if (!defined('EXPONENT')) exit('');
    global $user;
    if($_REQUEST['ajax_action']==1){
        if(exponent_users_isLoggedIn())
        {
            global $db,$browser;
            $config=$db->selectObject('profilemodule_global_config',"1=1");
            $mod=$_REQUEST['mod'];//invoice, case, document

            $profile_detail = $db->selectObject('profilemodule_profile_link',"'uid'= ".$user->id);       

            $template = new template('profilemodule','_cases_ajax',$loc);
            $template->assign('moduletitle',$title);
            //-------Requests--------
            $mod=$_REQUEST['mod'];
            if(isset($_REQUEST['search']))
                $mod=trim($_REQUEST['mod']);
            else
                return exponent_http_error(403);
            if(isset($_REQUEST['search']))
                $search=trim($_REQUEST['search']);
            else
                $search='';
            if(isset($_REQUEST['order_by']))
                $order=trim($_REQUEST['order_by']);
            else
                $order='date';
            if(!isset($_REQUEST['page']))
                $_REQUEST['page']=1;
            $max=$config->max_item_page;
            $start=($max*($_REQUEST['page']-1));
            //-------Requests--------
            $limit="LIMIT  $start, $max";             

            $sql=  "'name' LIKE %$search% AND uid'= ".$user->id;     
            $total =$db->countObjects('profilemodule_'.mod,$sql);
            $sql.="";
            $details=$db->selectObjects('profilemodule_'.mod,"'name' LIKE %$search% AND uid'= ".$user->id." ORDER BY $order $limit");

            foreach ($details as $value)
            {
                $value->date_create=date("d/m/Y", strtotime($value->date_entered));
                if($value->document_id)
                {
                    $value->decode_id=base64_encode($value->document_id.'|'.$value->file_ext.'|'.$value->document_name.'|'.$value->file_mime_type.'|'.md5(exponent_sessions_get('contact_id')));
                    if($mod!="document")
                        $value->file_type='pdf';
                }
            }
            $template->assign('start',$start);
            $template->assign('curent_page', $_REQUEST['page']);
            $template->assign('total',$total);
            $template->assign('details',$details);
            $template->register_permissions(array('administrate','configure','create','edit','delete'),$loc);
            echo $template->render();
        }
    }
    else
        exponent_http_error(403);
?>
